package com.boot.core.async.action;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 降级返回值
 * @date 2023/3/9 18:49
 **/

public abstract class AbstractFallbackHandler<R> {

    private final R fallbackValue;
    private final boolean nonNull;

    public AbstractFallbackHandler(R fallbackValue) {
        this(fallbackValue, true);
    }

    public AbstractFallbackHandler(R fallbackValue, boolean nonNull) {
        this.fallbackValue = fallbackValue;
        this.nonNull = nonNull;
    }

    protected R fallback(R result, Throwable throwable) {
        if (throwable != null) {
            return fallbackValue;
        }
        if (nonNull && result == null) {
            return fallbackValue;
        }
        return result;
    }

}
